
package fileby.gui.breadcrumbs;

import fileby.core.IDisplayableFileSource;
import fileby.ctrl.breadcrumbs.BreadCrumbListener;
import fileby.ctrl.breadcrumbs.IBreadCrumbListenerProvider;
import fileby.ctrl.transferhandlers.ITransferHandlerProvider;
import fileby.core.TransferHandlerType;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;

/**
 *
 * @author Jacob
 */
class BreadCrumb extends javax.swing.JPanel {

    private IDisplayableFileSource source;
    private Icon currentBackground;
    private IPropertyBundleProvider propertyBundleProvider;
    private ITransferHandlerProvider transferHandlerProvider;
    private IBreadCrumbListenerProvider listenerProvider;
    private Icon background;
    private Icon background_mouseover;
    private Icon background_pressed;

    public BreadCrumb(IDisplayableFileSource source, 
            IPropertyBundleProvider propertyBundleProvider,
            ITransferHandlerProvider transferHandlerProvider,
            IBreadCrumbListenerProvider listenerProvider) {

        this.propertyBundleProvider = propertyBundleProvider;
        this.transferHandlerProvider = transferHandlerProvider;
        this.listenerProvider = listenerProvider;
        this.source = source;
        initComponents();        
        initCtrl();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        currentBackground.paintIcon(this, g, 0, 0);
    }

    private void initCtrl() {
        setTransferHandler(transferHandlerProvider.create(TransferHandlerType.BREAD_CRUMB));
        BreadCrumbListener l = listenerProvider.create();
        addMouseListener(l);
        addMouseMotionListener(l);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                currentBackground = background_mouseover;
                repaint();
            }
            @Override
            public void mouseExited(MouseEvent e) {
                currentBackground = background;
                repaint();
            }
            @Override
            public void mousePressed(MouseEvent e) {
                currentBackground = background_pressed;
                repaint();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        icon = new javax.swing.JLabel();
        name = new javax.swing.JLabel();

        setInheritsPopupMenu(true);
        setMaximumSize(new java.awt.Dimension(140, 35));
        setMinimumSize(new java.awt.Dimension(140, 35));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(140, 35));

        PropertyBundle bundle = propertyBundleProvider.create(this.getClass());

        icon.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        icon.setMaximumSize(new java.awt.Dimension(30, 30));
        icon.setPreferredSize(new java.awt.Dimension(28, 32));

        name.setFont(bundle.getFont("name.font")); // NOI18N
        name.setForeground(bundle.getColor("name.foreground")); // NOI18N

        background = bundle.getIcon("background.icon");
        background_mouseover = bundle.getIcon("mouseover.icon");
        background_pressed = bundle.getIcon("pressed.icon");
        currentBackground = background;

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(name)))
                .addContainerGap())
        );

        icon.setIcon(source.getIcon());
        name.setText(source.getName());
        setToolTipText(source.getName());
        setName(source.toString());

    }// </editor-fold>
    
    private javax.swing.JLabel icon;
    private javax.swing.JLabel name;
    
}
